package com.example.book;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl implements BookDao{
    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/book?serverTimezone=Asia/Shanghai&characterEncoding=utf-8",
                "root","root"
        );
    }
    @Override
    public void addBook(Book book){
        try (Connection connection = getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(
                    "INSERT INTO book(title,author,price) VALUES (?,?,?)"
            )){
            preparedStatement.setString(1,book.getTitle());
            preparedStatement.setString(2,book.getAuthor());
            preparedStatement.setDouble(3,book.getPrice());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
                e.printStackTrace();
        }
    }

    @Override
    public void delBook(int id) {
        try (
            Connection connection=getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(
                    "DELETE FROM book WHERE id = ? "
            )){
            preparedStatement.setInt(1,id);
            preparedStatement.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Book getById(int id){
        Book book = null;
        try (
                Connection connection=getConnection();
                PreparedStatement preparedStatement=connection.prepareStatement(
                        "SELECT * FROM book WHERE id = ? "
                )){
            preparedStatement.setInt(1,id);
            ResultSet rs=preparedStatement.executeQuery();
            if(rs.next()){
                book = new Book(rs.getInt("id"), rs.getString("title"), rs.getString("author"), rs.getDouble("price"));
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return book;
    }
    @Override
    public List<Book> getAllBooks(){
        List<Book> books = new ArrayList<>();
        try (
                Connection connection=getConnection();
                PreparedStatement preparedStatement=connection.prepareStatement(
                        "SELECT * FROM book"
                )){
            ResultSet rs=preparedStatement.executeQuery();
            while (rs.next()){
                books.add(new Book(rs.getInt("id"),rs.getString("title"),rs.getString("author"),rs.getDouble("price")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return books;
    }
    @Override
    public void updateBook(Book book){
        try (
            Connection connection=getConnection();
            PreparedStatement preparedStatement= connection.prepareStatement(
                    "UPDATE book SET title = ? , author = ? , price = ? WHERE id = ? "
            )){
            preparedStatement.setString(1,book.getTitle());
            preparedStatement.setString(2,book.getAuthor());
            preparedStatement.setDouble(3,book.getPrice());
            preparedStatement.setInt(4,book.getId());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
